package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.shop.EbPopularAreas;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.EbPopularAreasRequest;
import com.zbkj.service.dao.shop.EbPopularAreasDao;
import com.zbkj.service.service.EbPopularAreasService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EbPopularAreasServiceImpl extends ServiceImpl<EbPopularAreasDao, EbPopularAreas>
        implements EbPopularAreasService {

    @Autowired
    private EbPopularAreasDao ebPopularAreasDao;

    /**
     * 新增热门地区分类
     *
     * @param ebPopularAreas 热门地区分类对象
     * @return 是否成功
     */
    @Override
    public EbPopularAreas add(EbPopularAreas ebPopularAreas) {
        log.info("开始新增热门地区分类: {}", ebPopularAreas);
        Date date = new Date();
        ebPopularAreas.setCreateTime(date);
        ebPopularAreas.setUpdateTime(date);
        ebPopularAreasDao.insert(ebPopularAreas);
        return ebPopularAreas;
    }

    /**
     * 更新热门地区分类
     *
     * @param ebPopularAreas 热门地区分类对象
     * @return 是否成功
     */
    @Override
    public EbPopularAreas update(EbPopularAreas ebPopularAreas) {
        log.info("开始更新热门地区分类: {}", ebPopularAreas);
        Date date = new Date();
        ebPopularAreas.setUpdateTime(date);
        ebPopularAreasDao.updateById(ebPopularAreas);
        return ebPopularAreas;
    }

    /**
     * 删除热门地区分类
     *
     * @param id 热门地区分类ID
     * @return 是否成功
     */
    @Override
    public boolean delete(Integer id) {
        log.info("开始删除热门地区分类, ID: {}", id);
        return ebPopularAreasDao.deleteById(id) > 0;
    }

    /**
     * 根据ID查询热门地区分类
     *
     * @param id 热门地区分类ID
     * @return 热门地区分类对象
     */
    @Override
    public EbPopularAreas getById(Integer id) {
        log.info("开始查询热门地区分类, ID: {}", id);
        return ebPopularAreasDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageInfo<EbPopularAreas> getPage(EbPopularAreasRequest request) {
        log.info("getPage: page={}, size={}", request.getPage(), request.getLimit());
        Page<EbPopularAreas> page = PageHelper.startPage(request.getPage(), request.getLimit());
        List<EbPopularAreas> list = ebPopularAreasDao.selectList(
                new LambdaQueryWrapper<EbPopularAreas>()
                        .eq(request.getPid() == null, EbPopularAreas::getPid, request.getPid())
                        .like(StringUtils.isNotBlank(request.getName()), EbPopularAreas::getName, request.getName())
        );

        return CommonPage.copyPageInfo(page, list);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public List<EbPopularAreas> findByList(EbPopularAreasRequest request) {
        List<EbPopularAreas> list = ebPopularAreasDao.selectList(
                new LambdaQueryWrapper<EbPopularAreas>()
                        .eq(EbPopularAreas::getPid, request.getPid())
                        .eq(EbPopularAreas::getCity, request.getCity())
        );

        return list;
    }


}
